<!--  -->
<template>
  <div class="musicList">
    <div class="musicTop">
      <div class="title">发现好歌单</div>
      <div class="more">查看更多</div>
    </div>
    <div class="musiContent">
      <van-swipe :loop="false" :width="150" class="my-swiper" :show-indicators="false">
        <van-swipe-item v-for="item in state.musicList" :key="item">
          <router-link :to="{ path: '/itemMusic', query: { id: item.id } }">
            <div class="imag">
              <img :src="item.picUrl" alt="">
              <span class="playCount">
                <svg class="icon" aria-hidden="true" style="color:#fff">
                  <use xlink:href="#icon-fangke"></use>
                </svg>
                {{ changeCount(item.playCount) }}
              </span>
            </div>
            <span class="name">{{ item.name }}</span>
          </router-link>
        </van-swipe-item>

      </van-swipe>

    </div>

  </div>
</template>

<script>
import { getMusicList } from '@/request/api/home'
import { onMounted, reactive } from 'vue'

export default {
  // data() {  //vue2写法
  //   return {
  //     musicList: []
  //   }
  // },
  // methods: {
  //   async getGedan() {
  //     let res = await getMusicList();
  //     console.log(res);
  //     this.musicList = res.data.result
  //   },
  //   changeCount: function (num) {
  //     if (num >= 100000000) {
  //       return (num / 100000000).toFixed(1) + '亿'
  //     } else if (num >= 10000) {
  //       return (num / 10000).toFixed(1) + '万'
  //     }
  //   }
  // },
  // mounted() {
  //   this.getGedan()
  // }
  //vue3写法
  setup() {
    const state = reactive({
      musicList: [],

    });
    function changeCount(num) {
      if (num >= 100000000) {
        return (num / 100000000).toFixed(1) + '亿'
      } else if (num >= 10000) {
        return (num / 10000).toFixed(1) + '万'
      }
    }
    onMounted(async () => {
      let res = await getMusicList();
      state.musicList = res.data.result

    });
    return { state, changeCount }
  },
}
</script>

<style lang="less" scoped>
.musicList {
  width: 100%;
  height: 5rem;
  padding: 0.2rem;

  .musicTop {
    width: 100%;
    height: 0.6rem;
    display: flex;
    justify-content: space-between;
    margin-top: 0.2rem;

    .title {
      font-size: 0.4rem;
      font-weight: 900;
    }

    .more {
      border: 1px solid #ccc;
      text-align: center;
      line-height: 0.6rem;
      padding: 0 0.2rem;
      border-radius: 0.4rem;
    }
  }

  .musiContent {
    width: 100%;
    height: 4rem;
    margin-top: 10px;

    .my-swiper {

      height: 100%;

      .van-swipe-item {
        height: 3rem;

        .imag {
          width: 90%;
          height: 90%;
          position: relative;
          border-radius: 10%;
          overflow: hidden;


          img {
            width: 100%;
            height: 3rem;
            position: absolute;
          }

          .playCount {
            position: absolute;
            left: .6rem;
            top: 0.2rem;
            color: #fff;
            font-size: .2rem;
            display: flex;
            justify-content: center;

            .icon {
              width: .4rem;
              height: .4rem;
              fill: #fff;
              margin-right: .1rem;
            }
          }
        }

        .name {

          display: flex;
          font-size: .3rem;
          color: #000;
        }
      }

    }
  }
}
</style>
